package com.miaopasi.securitydemo.controller;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController {

    @Autowired
    HttpServletRequest request;

    @GetMapping("/get")
    public String get() {
        return "success";
    }

    @GetMapping("/loginUser")
    public Authentication loginUser() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping("/code")
    public String getCode() {
        String numbers = RandomUtil.randomStringUpper(6);
        request.getSession().setAttribute("code", numbers);
        return numbers;
    }
}
